package com.lottery.im.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.lottery.im.R;
import com.lottery.im.util.ScreenUtils;

public class BottomWebViewMoreDialog {

  private static Dialog mDialog;
  private Context mContext;

  private int[] iconRes = { R.drawable.btn_bcfc, R.drawable.btn_sx, R.drawable.btn_fhsj };
  private String[] titles = { "浮窗", "刷新", "返回上级" };
  private AdapterView.OnItemClickListener mOnItemClickListener;

  public Dialog create(Context context, int type,
      AdapterView.OnItemClickListener onItemClickListener) {
    mDialog = new Dialog(context, R.style.BottomSelectDialogTheme);
    this.mContext = context;
    this.mOnItemClickListener = onItemClickListener;
    if (type == 1) {
      titles[0] = "取消浮窗";
    }

    Window window = mDialog.getWindow();
    window.requestFeature(Window.FEATURE_NO_TITLE);
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    mDialog.setContentView(R.layout.dialog_bottom_webview_more);
    window.setGravity(Gravity.BOTTOM);
    window.setWindowAnimations(R.style.bottom_animation);
    WindowManager.LayoutParams params = window.getAttributes();
    params.width = ScreenUtils.getWidth(context);
    window.setAttributes(params);

    GridView gridView = mDialog.findViewById(R.id.grid);
    gridView.setAdapter(new GridMoreAdapter());
    gridView.setOnItemClickListener((parent, view, position, id) -> {
      if (null != onItemClickListener) onItemClickListener.onItemClick(parent, view, position, id);
      dismiss();
    });

    mDialog.findViewById(R.id.btn_cancel).setOnClickListener(v -> mDialog.dismiss());
    return mDialog;
  }

  public static void dismiss() {
    if (mDialog != null) {
      mDialog.dismiss();
    }
    mDialog = null;
  }

  class GridMoreAdapter extends BaseAdapter {

    @Override public int getCount() {
      return iconRes.length;
    }

    @Override public Object getItem(int position) {
      return iconRes[position];
    }

    @Override public long getItemId(int position) {
      return position;
    }

    @Override public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder holder;
      if (null == convertView) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_webview_more, null);
        holder = new ViewHolder();
        holder.tvTitle = convertView.findViewById(R.id.tv_title);
        holder.ivIcon = convertView.findViewById(R.id.iv_icon);
        convertView.setTag(holder);
      } else {
        holder = (ViewHolder) convertView.getTag();
      }
      holder.ivIcon.setImageResource(iconRes[position]);
      holder.tvTitle.setText(titles[position]);

      return convertView;
    }

    class ViewHolder {
      TextView tvTitle;
      ImageView ivIcon;
    }
  }
}
